﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.DAL;
using AiXiu.Model;

namespace AiXiu.BLL
{
    public class LikeManger
    {
        string keyPreStr = "VideoLike";
        /// <summary>
        /// 点赞计数
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public long Count(string videoId)
        {
            string key = $"{keyPreStr}_ {videoId}";
            return RedisHelper.SetLength(key);
        }
        /// <summary>
        /// 当前用户是否已点赞
        /// </summary>
        /// <param name="videoId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool IsLike(string videoId, int userId)
        {
            string key = $"{keyPreStr}_ {videoId}";
            return RedisHelper.SetContains(key, userId);    
        }
        /// <summary>
        ///当前登录用户添加点赞
        /// </summary>
        /// <param name="videoId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public OperResult AddLike(string videoId, int userId)
        {
            string key = $"{keyPreStr}_ {videoId}";
            bool result=RedisHelper.SetAdd(key, userId);
            if (result)
            {
                return OperResult.Succeed();
            }
            else
            {
                return OperResult.Failed("操作失败");
            }
        }
        /// <summary>
        /// 当前登录用户移出点赞
        /// </summary>
        /// <param name="videoId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public OperResult RemoveLike(string videoId, int userId)
        {
            string key = $"{keyPreStr}_ {videoId}";
            bool result = RedisHelper.SetRemove(key, userId);
            if (result)
            {
                return OperResult.Succeed();
            }
            else
            {
                return OperResult.Failed("操作失败");
            }
        }
    }
}
